#!/usr/bin/env python
# -*- coding: utf-8 -*-
# title       :
# author      : lee

import sys
import os
import logging
import re
from SceneCheckTool.checkScript.CheckBase import CheckBase

try:
    import maya.cmds as cmds
    import pymel.core as pm
    import jarryLibs.mayakit.MayaUtils as mu
    reload(mu)
except:
    pass

logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)


class CfxPgYetiGraphInputCheck(CheckBase):
    # 初始化检查属性表
    import_types_attr_lib = {0: "inputGeometry", 1: "inputStrandData", 2: "guideSets"}

    def __init__(self):
        super(CfxPgYetiGraphInputCheck, self).__init__()

    def check_pgYeti_node_attrs(self):
        """
        描述：pgYeti节点检查的启动函数
        """
        error_nodes = []
        for yeti_node in cmds.ls(type="pgYetiMaya"):
            # 检查Graph参数中的input节点是否存在
            miss_connection_nodes = self.check_pgYeti_graph(yeti_node)
            if miss_connection_nodes:
                error_nodes.append(yeti_node)
                continue

        return error_nodes

    def check_pgYeti_graph(self, node):
        """
        描述：检查pgYeti的Graph组中的Input链接的节点是否存在
        """
        miss_nodes = []
        check_attrs = ["inputGeometry", "inputStrandData", "guideSets"]
        for attr in check_attrs:
            if not cmds.listConnections(node + "." + attr):
                continue
            for connect_node in cmds.listConnections(node + "." + attr):
                if not cmds.objExists(connect_node):
                    miss_nodes.append(connect_node)
        return miss_nodes

    def check_pgYeti_root_node(self, node):
        root_node = cmds.pgYetiGraph(node, getRootNode=True)
        if not root_node:
            return False
        if cmds.pgYetiGraph(node, node=root_node, outputNodeCount=True) > 0:
            return False
        return True

    def check(self):
        result_proc = self.check_pgYeti_node_attrs()
        if result_proc:
            result_log = u'Error:%s\n' % result_proc
        else:
            result_log = u'Succeed:没有问题\n'

        return result_log

    def fix(self):
        pass
